package gui.hug;

  import flash.display.Sprite;
  import flash.events.Event;
  import flash.events.MouseEvent;
  import flash.display.InteractiveObject;
  import flash.events.TextEvent;
  import flash.events.KeyboardEvent;
  import flash.text.TextField;
  import flash.text.TextFormat;
  import gui.hug.HUGScrollBar;
  import org.flashdevelop.utils.FlashConnect;

  /**
  * @author Andrew Rogozov
  * @modified by Alexey Kharkov
  * @modified by Natalia Ukhorskaya
  */
class HUGInputFieldSB extends HUGSprite
  {
	public static inline var EVENT_MODIFIED : String = "modified";
    
    private static inline var STD_HEIGHT : UInt = 21;
    private static inline var FONT_SZ : Int = 11;
    private static inline var LINE_H : Int = FONT_SZ + 2;
    
    private var tf : TextField;
    private var editable : Bool;
		private var sb : HUGScrollBar;
    private var multiline : Bool;
    private var w : Int;
    private var pgSz : Int;

    public function new (x : Int, y : Int, width : Int, ?linesCount : Int = 1, ?editable : Bool = true, ?border : Bool = true )
    {
		super();
		
		tf = null;
			sb = null;
		multiline = false;
		w = 0;
		pgSz = 0;
	
      this.x = x;
      this.y = y;
      this.w = width;
      this.editable = editable;
      
      pgSz = linesCount * LINE_H;
      var height:UInt = 6 + pgSz;
      
			var flags : UInt = editable ? Utils.TXT_INPUT : 0;
      
      if (height < STD_HEIGHT)
        height = STD_HEIGHT;
      else
      if (height > STD_HEIGHT)
      {
        multiline = true;
        flags |= Utils.TXT_MULTILINE;
      }
      
      if (border)
        Utils.rect(this, 0, 0, width, height, 0xFFFFFF, 0xc0cad5);
      
      tf = Utils.addText(4, 2, width - 2, 11, "", 0x000000, flags, height - 2);
      tf.selectable = true;
			tf.maxChars = 0x7fff;
      addChild( tf );
      
      // Add ScrollBar for multiline InputField
      if (multiline  &&  height > HUGScrollBar.MIN_H)
      {
        sb = new HUGScrollBar(width - HUGScrollBar.W, 0, height);
        updSb();
        
        sb.lineScrollSize = LINE_H * 2;
        sb.addEventListener(Event.SCROLL, onBarScroll);
        addChild(sb);
        
		//TODO
        tf.addEventListener( MouseEvent.ROLL_OVER, function(e:Event){ MouseWheel.capture(); } );
        tf.addEventListener( MouseEvent.ROLL_OUT , function(e:Event){ MouseWheel.release(); } );
        
        tf.addEventListener(Event.SCROLL, onTextScroll);
        if (editable)
          tf.addEventListener(Event.CHANGE, onTextChange);
      }
      
      if (editable)
        tf.addEventListener(KeyboardEvent.KEY_DOWN, onKey);
    }
    
    public function setFocus()
    {
      tf.stage.focus = tf;
      tf.setSelection(tf.length, tf.length);
    }

    public function getTextField():TextField
    {
      return tf;
    }

    public function getValue(): String
    {
      return tf.text;
    }

    public function setValue(text:String)
    {
      tf.text = text;
      updSb();
    }
    
    public function getWidth():Int
    {
      return w;
    }
    
    public function getHeight():Float
    {
      return tf.height;
    }
    
    public function getTextHeight():Float
    {
      return tf.textHeight;
    }
    
    // -------------------------------------------------------------------- Event handlers
		private function updSb() // Update ScrollBar
		{
      if (sb == null)
        return;
      
			if (tf.textHeight <= tf.height)
      {
				sb.visible = false;
        tf.width = w - 2;
        return;
      }

      tf.width = sb.x - 2;
      tf.text = tf.text;

      var maxPos:Float = tf.textHeight - pgSz;
      //var maxPos:Number = LINE_H * tf.numLines - pgSz;

      sb.init(Math.round(maxPos), pgSz);
      sb.visible = true;
		}

		private function onTextChange(e : Event)
		{
			updSb();
		}

		private function onKey(e:KeyboardEvent)
		{
			if (e.keyCode == 13  &&  !multiline)// &&  (e.ctrlKey  ||  !multiline) )
        dispatchEvent(new Event(EVENT_MODIFIED));
		}

    private function onBarScroll(e:Event)
    {
      tf.removeEventListener(Event.SCROLL, onTextScroll);
      tf.scrollV = 1 + Math.ceil(sb.getScrollPosition() / LINE_H);
      tf.addEventListener(Event.SCROLL, onTextScroll);
    }
    
    public function onTextScroll(e:Event)
    {
      sb.setScrollPosition(Math.round((tf.scrollV - 1) * LINE_H));
    }

  }